
export const downloadPdf = (fileUrl) => {
    uni.showLoading({
        title: "下载中",
    });
    uni.downloadFile({
        url: fileUrl,
        success: (res) => {
            wx.getFileSystemManager().saveFile({
                tempFilePath: res.tempFilePath,
                success: function (res) {
                    const savedFilePath = res.savedFilePath;
                    uni.hideLoading();
                    uni.showToast({
                        title: "下载成功",
                        icon: "success",
                    });
                    uni.openDocument({
                        filePath: savedFilePath,
                        fileType: "pdf",
                        showMenu: true,
                        success: function (res) {
                        },
                        fail: (err) => {
                        },
                    });
                },
            });
        },
    });
};

export const downloadImg = (imgUrl) => {
    wx.showLoading({
        title: '加载中...'
    });
    if (typeof imgUrl === 'string') {
        // 下载图片
        wx.downloadFile({
            url: imgUrl, //图片地址
            success: function (res) {
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath, //图片文件路径
                    success: function () {
                        wx.hideLoading(); //隐藏 loading 提示框
                        uni.showModal({
                            title: '提示',
                            content: '保存成功',
                            showCancel: false,
                        })
                    },
                    // 接口调用失败的回调函数
                    fail: function (err) {
                        saveImageToPhotosAlbumFail(err)
                    },
                    complete(res) {
                        wx.hideLoading(); //隐藏 loading 提示框
                    }
                })
            }
        })
    } else {
        let saveSuccessCount = 0;
        let saveFailCount = 0;
        imgUrl.forEach(item => {
            wx.downloadFile({
                url: item, //图片地址
                success: function (res) {
                    wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath, //图片文件路径
                        success: function () {
                            saveSuccessCount++;
                        },
                        // 接口调用失败的回调函数
                        fail: function (err) {
                            saveFailCount++
                            saveImageToPhotosAlbumFail(err)
                        },
                        complete(res) {
                            if ((saveSuccessCount + saveFailCount) === imgUrl.length) {
                                wx.hideLoading(); //隐藏 loading 提示框
                                uni.showModal({
                                    title: '提示',
                                    content: '保存成功',
                                    showCancel: false,
                                })
                            }
                        }
                    })
                }
            })
        })
    }
}

const saveImageToPhotosAlbumFail = (err) => {
    if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
        wx.showModal({
            title: '提示',
            content: '需要您授权保存相册',
            modalType: false,
            success: modalSuccess => {
                wx.openSetting({
                    success(settingdata) {
                        if (settingdata.authSetting['scope.writePhotosAlbum']) {
                            wx.showModal({
                                title: '提示',
                                content: '获取权限成功,再次点击下载即可保存',
                                modalType: false,
                            })
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '获取权限失败，将无法保存到相册哦~',
                                modalType: false,
                            })
                        }
                    },
                    fail(failData) {
                    },
                    complete(finishData) {
                    }
                })
            }
        })
    }
}
// 防抖
export function debounce(func, wait) {
    let timeout;
    return function(...args) {
      const context = this;
      clearTimeout(timeout);
      timeout = setTimeout(() => func.apply(context, args), wait);
    };
}